package com.eight.vote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eight.vote.pojo.Category;
import com.eight.vote.mapper.CategoryMapper;
import com.eight.vote.pojo.VMessage;
import com.eight.vote.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eight.vote.service.VMessageService;
import com.eight.vote.util.Result;
import com.eight.vote.util.UpdateStatus;
import com.eight.vote.vo.CategoryNumVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 点点星言
 * @since 2022-10-16
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    VMessageService messageService;

    @Override
    public Result countFromDB() {
        //在返回数据前更新一次数据看看是否已过投票期
        UpdateStatus.changeStatus(messageService);
        //先将整个活动类型表全部查出来
        List<Category> categoryList = baseMapper.selectList(null);
        List<CategoryNumVo> numVos = new ArrayList<>();
        //分别检索每个不同状态的数量，然后封装进list中
        //调用对应的方法检索对应的状态
        CategoryNumVo statusNumber1 = getStatusNumber(0L);
        CategoryNumVo statusNumber2 = getStatusNumber(1L);
        CategoryNumVo statusNumber3 = getStatusNumber(2L);
        statusNumber1.setName("未开始");
        statusNumber2.setName("进行中");
        statusNumber3.setName("已结束");

        //通过活动类型表的id去查询整个投票信息表，将其对应的所有数量查询出来
         numVos = categoryList.stream().map(item -> {
            QueryWrapper<VMessage> wrapper = new QueryWrapper<>();
            wrapper.eq("category_id", item.getCategoryId());
            int count = (int) messageService.count(wrapper);
            //再将查询到的活动类型表的id和类型名称和数量封装起来，
            CategoryNumVo categoryNumVo = new CategoryNumVo();
            categoryNumVo.setId(item.getCategoryId());
            categoryNumVo.setName(item.getCategoryName());
            categoryNumVo.setNumber(count);
            categoryNumVo.setIsStatus(1);
            return categoryNumVo;
        }).collect(Collectors.toList());
        numVos.add(statusNumber1);
        numVos.add(statusNumber2);
        numVos.add(statusNumber3);

        //将整个list返回
        return Result.ok(numVos);
    }

    @Override
    public Result getAllCategory() {
        List<Category> categories = baseMapper.selectList(null);
        return Result.ok(categories);
    }

    private CategoryNumVo getStatusNumber(Long number) {
        QueryWrapper<VMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("status",number);
        int notBeginCount = (int) messageService.count(wrapper);
        CategoryNumVo notBegin =  new CategoryNumVo();

        notBegin.setId(number);
        notBegin.setNumber(notBeginCount);
        notBegin.setIsStatus(0);
        return notBegin;
    }


}
